package com.huafeng.function;

import com.huafeng.config.BotPropertiesConfig;
import com.huafeng.constant.RequestConstant;
import com.huafeng.entity.GroupProperties;
import com.huafeng.utils.HttpUtils;
import com.huafeng.utils.MessageUtils;
import com.huafeng.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @author 花风
 * @date 2023/4/6 21:44
 * @Description 请求处理类
 */
@Slf4j
public class RequestFunctions {
    private static final BotPropertiesConfig botPropertiesConfig = SpringContextUtils.getBean(BotPropertiesConfig.class);
    private static final Map<String,GroupProperties> groupNums = botPropertiesConfig.getManage_groups();

    public static Object Friend(List<String> params){
        //获取参数
        String flag = params.get(1);
        String comment = params.get(2);

        String url = MessageUtils.BASE_URL + RequestConstant.ADD_FRIEND_REQUEST;
        Map<String,Object> map = new HashMap<>();
        map.put("flag",flag);
        map.put("approve",true);
        map.put("remark",comment);

        return HttpUtils.doGet(url,map);
    }

    public static Object Group(List<String> params){
        //获取参数
        String groupId = params.get(0);
        String flag = params.get(1);
        String comment = params.get(2).substring(params.get(2).lastIndexOf('：') + 1);
        String subType = params.get(3);

        log.info(groupNums.toString());
        log.info(String.valueOf(groupNums.containsKey(groupId)));
        log.info(String.valueOf(groupNums.get(groupId).getAnswers().contains(comment)));
        Object result = null;
        if (groupNums.containsKey(groupId)){
            //只有在管理的群里面才进行处理
            String url = MessageUtils.BASE_URL + RequestConstant.ADD_GROUP_REQUEST;
            Map<String,Object> map = new HashMap<>();
            map.put("flag",flag);
            log.info("approve = " + groupNums.get(groupId).getAnswers().contains(comment));
            //判断答案是否正确
            if (groupNums.get(groupId).getAnswers().contains(comment)){
                map.put("approve",true);
            }else {
                map.put("approve",false);
                map.put("reason","答案错误");
            }
            map.put("sub_type",subType);
            result = HttpUtils.doGet(url,map);
        }
        return result;
    }
}
